import Host from "@/views/host.vue";
import Logo from "@/views/logo.vue";
import People from "@/views/people.vue";
import Zhuce from "@/views/zhuce.vue";
import Vue from "vue";
import VueRouter from "vue-router";

Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    meta: { title: "注册" },
    redirect: "zhuce",
  },
  {
    path: "/zhuce",
    name: "zhuce",
    meta: { title: "注册" },
    component: Zhuce,
  },
  {
    path: "/logo",
    name: "logo",
    meta: { title: "登录" },
    component: Logo,
  },
  {
    path: "/host",
    name: "host",
    meta: { title: "主页" },
    component: Host,
    beforeEnter: (to, from, next) => {
      if (localStorage.getItem("username") && localStorage.getItem("password")) {
        next();
      } else {
        next({ path: "/zhuce" }, alert("请先注册"));
      }
    },
  },
  {
    path: "/geren",
    name: "geren",
    meta: { title: "个人中心" },
    component: People,
  },
  {
    path: "*",
    meta: { title: "404" },
    component: () =>
      import(/* webpackChunkName: "Error" */ "../views/Error.vue"),
  },
];

const router = new VueRouter({
  routes,
  mode: "history",
});

router.beforeEach((to, from, next) => {
  if (to.meta && to.meta.title) {
    document.title = to.meta.title;
  }
  next();
});

export default router;
